﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("HHMMS.Core.Web.WebControls.Js.SmartPicker.js", "application/x-javascript")]
namespace HHMMS.Core.WebControls
{
    /// <summary>
    /// 封装选分类、选商品的方法。
    /// </summary>
    public class SmartPicker : CompositeControl 
    {
        private string url = string.Empty;
        private TextBox tbText = new TextBox();
        private TextBox tbID = new TextBox();
        private Image image = new Image();

        [DefaultValue(SelectMode.LayoutSelect)]
        [PersistenceMode(PersistenceMode.Attribute)]
        public SelectMode SelectMode
        {
            get;
            set;
        }

        [DefaultValue("~/Images/ico_search.gif")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public string TriggerImageUrl
        {
            get;
            set;
        }

        [PersistenceMode(PersistenceMode.Attribute)]
        public string TriggerToolTip
        {
            get;
            set;
        }

        [PersistenceMode(PersistenceMode.Attribute)]
        public string Values
        {
            get
            {
                return tbID.Text;
            }
            set
            {
                tbID.Text = value;
            }
        }

        [PersistenceMode(PersistenceMode.Attribute)]
        public string Text
        {
            get
            {
                return tbText.Text;
            }
            set
            {
                tbText.Text = value;
            }
        }

        [DefaultValue(420)]
        [PersistenceMode(PersistenceMode.Attribute)]
        public int? FormWidth
        {
            get;
            set;
        }

        [DefaultValue(420)]
        [PersistenceMode(PersistenceMode.Attribute)]
        public int? FormHeight
        {
            get;
            set;
        }

        [DefaultValue(false)]
        [PersistenceMode(PersistenceMode.Attribute)]
        public bool? AllowMulti
        {
            get;
            set;
        }

        [DefaultValue("")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Query
        {
            get;
            set;
        }

        [DefaultValue(false)]
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Validator
        {
            get;
            set;
        }

        [DefaultValue(false)]
        [PersistenceMode(PersistenceMode.Attribute)]
        public string TextField
        {
            get;
            set;
        }

        [DefaultValue(false)]
        [PersistenceMode(PersistenceMode.Attribute)]
        public string ValueField
        {
            get;
            set;
        }

        [PersistenceMode(PersistenceMode.Attribute)]
        public string OnClientPicked
        {
            get;
            set;
        }

        public List<ListItem> SelectedItems
        {
            get
            {
                var items = new List<ListItem>();
                if (!string.IsNullOrEmpty(this.Values))
                {
                    var ids = this.Values.Split(';');
                    var texts = this.Text.Split(';');
                    var index = 0;
                    foreach (string id in ids)
                    {
                        items.Add(new ListItem() { Value = id, Text = texts[index] });
                        index++;
                    }
                }
                return items;
            }
            set
            {
                if (value != null)
                {
                    var index = 0;
                    foreach (var item in value)
                    {
                        this.Text = string.Format("{0}{1}{2}", this.Text, index == 0 ? "" : ";", item.Text);
                        this.Values = string.Format("{0}{1}{2}", this.Values, index == 0 ? "" : ";", item.Value);
                        index++;
                    }
                }
            }
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            //创建隐藏域用来保存选择的值
            tbID.Text = this.Values;
            tbID.ID = "SelectedID";
            tbID.Attributes["style"] = "display:none";
            if (!string.IsNullOrEmpty(this.ValueField))
            {
                tbID.Attributes["Name"] = this.ValueField;
            }
            this.Controls.Add(tbID);

            //创建文本框用来显示选择的文本
            tbText.Text = this.Text;
            tbText.ID = "SelectedText";
            tbText.Attributes["validator"] = this.Validator;
            if (!string.IsNullOrEmpty(this.TextField))
            {
                tbText.Attributes["Name"] = this.TextField;
            }
            this.Controls.Add(tbText);

            //添加一个空格
            Literal lit = new Literal();
            lit.Text = "&nbsp;&nbsp;&nbsp;";
            this.Controls.Add(lit);

            //创建图片按钮用于出发弹出窗体事件
            image.ImageAlign = ImageAlign.AbsMiddle;
            image.ImageUrl = this.TriggerImageUrl;
            image.ToolTip = this.TriggerToolTip;
            image.Attributes["style"] = "cursor:pointer";
            image.Attributes["onclick"] = string.Format("showSmartPickerPage(this, '{0}', {1}, {2}, {3})", url, this.FormWidth.Value, this.FormHeight.Value, this.OnClientPicked ?? "null");
            this.Controls.Add(image);

            this.ClearChildViewState();
        }

        //初始化属性
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            switch (this.SelectMode)
            {
                case WebControls.SelectMode.LayoutSelect:
                    this.FormWidth = this.FormWidth ?? 420;
                    this.FormHeight = this.FormHeight ?? 430;
                    this.TriggerToolTip = this.TriggerToolTip ?? "点击选择布局";
                    this.TriggerImageUrl = this.TriggerImageUrl ?? "~/Images/ico_search.gif";
                    this.AllowMulti = this.AllowMulti ?? false;
                    this.url = Util.ContextPath + "/Modules/Common/LayoutSelect.aspx?allowMulti=" + (this.AllowMulti.Value ? "1" : "0") + "&query=" + HttpUtility.UrlEncode(this.Query);
                    break;
                case WebControls.SelectMode.CategorySelect:
                    this.FormWidth = this.FormWidth ?? 420;
                    this.FormHeight = this.FormHeight ?? 430;
                    this.TriggerToolTip = this.TriggerToolTip ?? "点击选择分类";
                    this.TriggerImageUrl = this.TriggerImageUrl ?? "~/Images/ico_search.gif";
                    this.AllowMulti = this.AllowMulti ?? false;
                    this.url = Util.ContextPath + "/Modules/Common/CategorySelect.aspx?allowMulti=" + (this.AllowMulti.Value ? "1" : "0") + "&query=" + HttpUtility.UrlEncode(this.Query);
                    break;
                case WebControls.SelectMode.DiscardPartsSelect:
                    this.FormWidth = this.FormWidth ?? 850;
                    this.FormHeight = this.FormHeight ?? 400;
                    this.TriggerToolTip = this.TriggerToolTip ?? "点击选择已报废配件";
                    this.TriggerImageUrl = this.TriggerImageUrl ?? "~/Images/ico_search.gif";
                    this.AllowMulti = this.AllowMulti ?? false;
                    this.url = Util.ContextPath + "/Modules/Common/DiscardPartsChoice.aspx?allowMulti=" + (this.AllowMulti.Value ? "1" : "0") + "&query=" + HttpUtility.UrlEncode(this.Query);
                    break;
                case WebControls.SelectMode.ProvinceCityAreaSelect:
                    this.FormWidth = this.FormWidth ?? 420;
                    this.FormHeight = this.FormHeight ?? 430;
                    this.TriggerToolTip = this.TriggerToolTip ?? "点击选择地区";
                    this.TriggerImageUrl = this.TriggerImageUrl ?? "~/Images/ico_search.gif";
                    this.AllowMulti = this.AllowMulti ?? false;
                    this.url = Util.ContextPath + "/Modules/Common/ProvinceCityAreaSelect.aspx?allowMulti=" + (this.AllowMulti.Value ? "1" : "0") + "&query=" + HttpUtility.UrlEncode(this.Query);
                    break;
            }
        }

        //引入脚本资源
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), "HHMMS.Core.Web.WebControls.Js.SmartPicker.js");
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute("height", "26px");
            writer.AddStyleAttribute("line-height", "26px");
            writer.AddStyleAttribute("vertical-align", "middle");
            if (this.Width != null && this.Width.Value > 50)
            {
                tbText.Width = new Unit(this.Width.Value - 50);
            }
            base.RenderBeginTag(writer);
        }
    }

    public enum SelectMode
    {
        /// <summary>
        /// 布局选择。
        /// </summary>
        LayoutSelect,

        /// <summary>
        /// 商品分类选择。
        /// </summary>
        CategorySelect,

        /// <summary>
        /// 已报废配件选择。
        /// </summary>
        DiscardPartsSelect,

        /// <summary>
        /// 地区选择。
        /// </summary>
        ProvinceCityAreaSelect
    }
}
